package com.white.movies.Utils;

import android.content.Context;

import com.google.gson.Gson;
import com.white.movies.Domains.MoviesBean;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

public class JsonUtils {
    // 从assets读取JSON并解析为Movie对象
    public static MoviesBean getMovieFromJson(Context context, String jsonFileName) {
        try {
            // 读取assets中的JSON文件
            InputStream is = context.getAssets().open(jsonFileName);
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            String json = new String(buffer, StandardCharsets.UTF_8);

            // Gson解析为Movie对象
            Gson gson = new Gson();
            return gson.fromJson(json, MoviesBean.class);

        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}